package io.terminus.lego.shinda.service.service

import io.terminus.lego.shinda.service.dao.SpecialUserRepository
import io.terminus.lego.shinda.service.dao.UserRepository
import io.terminus.lego.shinda.service.model.SpecialUser
import io.terminus.lego.shinda.service.model.User
import org.springframework.data.domain.Example
import org.springframework.stereotype.Service

/**
 * 特殊用户服务类
 * @author wangmeng
 * @date 2018/4/28
 */
@Service
class SpecialUserService constructor(val specialUserRepository: SpecialUserRepository,
                                     val userRepository: UserRepository) {

    fun findUserByUsernameAndType(username: String, type: String): User? {
        val user = SpecialUser(username = username, userType = type)
        val optional = specialUserRepository.findOne(Example.of(user))
        if (optional.isPresent) {
            return userRepository.getOne(optional.get().userId)
        }
        return null
    }
}